﻿
@{
    Layout = null;
}

<div>
    <div class="col-3 pr-0" style="display: none">
        <label id="SAMPLE-Runtime-SelectSamplinLab-Ids">@ViewData["Ids"]</label>
        <label id="SAMPLE-Runtime-SelectSamplinLab-LabType">@ViewData["LabType"]</label>
        <label id="SAMPLE-Runtime-SelectSamplinLab-Parent-ModalID">@ViewData["ModalID"]</label>
    </div>
</div>
<div class="input-group">
    <div class="col-12 pr-0 pl-0">
        <span class="input-group-text font-rfs border-radius-noright">标签列表</span>
    </div>
</div>
<div class="table-responsive-sm modal-table-shadow p-0 bg-white rounded col-md-12 pre-scrollable" style="height: 60vh;">
    <table id="SAMPLE-Runtime-SelectSamplinLab-Type" class="table table-striped table-bordered-modal table-sm table-hover">
        <thead>
            <tr>
                <th></th>
                <th>标签名称</th>
            </tr>
        </thead>
        <tbody style="font-size: calc(0.6em + 0.1vw) !important"></tbody>
    </table>
</div>
<div class="text-right" style="padding-top: 5px;">
    <button id="SAMPLE-Runtime-SelectSamplinLab-confirm" type="button" class="btn btn-primary font-rfs" disabled="disabled">确定</button>
    <button id="SAMPLE-Runtime-SelectSamplinLab-cancel" type="button" class="btn btn-info font-rfs">取消</button>
</div>

<script src="~/js/site.js"></script>
<script>

    $(document).ready(function () {
        LoadLab();
    });

    function LoadLab() {
        var columns = [{ field: "use", width: "5%", align: "center", radio: true }, { field: "type" }];

        $('#SAMPLE-Runtime-SelectSamplinLab-Type').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
        $('#SAMPLE-Runtime-SelectSamplinLab-Type').bootstrapTable({
            method: 'post',
            url: "GetLabList",
            dataType: "json",  // 服务器端返回值类型
            contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
            queryParams: function (params) {
                var data = {
                    LabType: $("#SAMPLE-Runtime-SelectSamplinLab-LabType").html(),
                };
                return data;
            }, //参数
            queryParamsType: "search", //参数格式,发送标准的RESTFul类型的参数请求

            uniqueId: "id",          // 设置主键
            locale: "zh-CN",
            columns: columns,
            selectItemName: "use", // 指定选择行ID
            clickToSelect:true, // 相应单击，自动选中此行
            checkboxHeader:false, // 标题行显示全选按钮
            singleSelect: true, // 单选
            silent: true,  //刷新事件必须设置
            onClickRow: function (row, $element, field) {
                $("#SAMPLE-Runtime-SelectSamplinLab-Type").find("tbody").find("tr").each(function () {
                    $(this).removeClass('changeblueColor');
                });
                $element.addClass("changeblueColor");
                $("#SAMPLE-Runtime-SelectSamplinLab-confirm").attr("disabled", false);
            },
            formatLoadingMessage: function () {
                return "请稍等，正在加载中...";
            },
            formatNoMatches: function () {  //没有匹配的结果
                return '无符合条件的记录';
            },
            onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                $('.fixed-table-toolbar').css("height", "0px"); /* 隐藏表格上的工具栏 */
                $('th label').css("margin-bottom", "0px"); // 设置标题行选择控件的样式
                $('td label').css("margin-bottom", "0px"); // 设置数据行选择控件的样式
            },
            onLoadError: function (data) {
                $('#SAMPLE-Runtime-SelectSamplinLab-Type').bootstrapTable('removeAll');
            }
        });
    }

    $("#SAMPLE-Runtime-SelectSamplinLab-cancel").click(function () {
        var modalID = $("#SAMPLE-Runtime-SelectSamplinLab-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

    $("#SAMPLE-Runtime-SelectSamplinLab-confirm").click(function () {
        var st = ""; // 获取表格中已选择的标签名称
        $("#SAMPLE-Runtime-SelectSamplinLab-Type > tbody tr").each(function(){
            if ($(this).find("td:eq(0)").find("input").prop("checked")) {
                st = st + "," + $(this).find("td:eq(1)").text();
            }
        });
        st = st.substring(1, st.lengths);

        $.post("CreateSamplingLab", {
            Ids: $("#SAMPLE-Runtime-SelectSamplinLab-Ids").html(),
            labName: st
        }, function (data) {
            if (data.indexOf("成功") >= 0) {
                var fileName = data.split(',')[1];
                var mime = getMIMEType(fileName);

                var formData = new FormData();
                formData.append("pathName", "SamplingLab_Path");
                formData.append("fileName", fileName);
                formData.append("isTemp", "True");

                const xhr = new XMLHttpRequest();
                xhr.open('POST', '/Common/DownloadFile', true);
                xhr.responseType = "blob";  // 这个是关键
                xhr.onload = () => {
                    //'\ufeff' 这个变量是定义字符集为utf-8, 防止中文乱码的问题。
                    const blob = new Blob(["\ufeff", xhr.response], { type: mime });
                    const blobUrl = URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.style.display = 'none';
                    a.href = blobUrl;
                    a.download = st; // 文件名
                    a.target = '_blank';
                    a.click();
                }
                xhr.send(formData);
                $("#SAMPLE-Runtime-SelectSamplinLab-cancel").click();
            } else {
                alert(data);
            }
        });

    });

</script>
